package com.yonyougov.devops.cruxcisupport.util;

import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.id.IdGenerator;

import java.math.BigInteger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class CruxDevOpsIdGenerator implements IdGenerator<BigInteger> {
    private static AtomicInteger seq = new AtomicInteger(0);

    private static long workId = new Random(System.nanoTime()).nextInt(2048);

    @Override
    public BigInteger generatorId(BaseDao baseDao) {
        long idValue = System.currentTimeMillis() << 21 | workId << 10 | seq.getAndUpdate(operand -> ++operand % 1024);
        return BigInteger.valueOf(idValue);
    }
}
